function [sandbox prms] = fsb_import_sandbox(sandbox,idat)

% FSB :  Import sandbox variables into sandbox
%
% EXAMPLE: [sandbox prms] = fsb_import_sandbox(sandbox,idat)
%
% INPUT:
% sandbox: sandbox struct
% idat: 4D image data
%
% OUTPUT:
% sandbox: same sandbox struct
% prms: parameters for sandbox
% 
% CALLED BY:
% FSB.m
% fsb_get_hemodynamics_er.m
% fsb_load_files.m
%
% NOTES:
% This is just used to make sure that the display parameters are set
% correctly
%
% Copyright 2010 MPI for Biological Cybernetics
% Author: Steffen Stoewer
% License:GNU GPL, no express or implied warranties
% 
% $ Revision 1.0
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

%~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Check if onset vector present
%~~~~~~~~~~~~~~~~~~~~~~~~~~~
try
    sandbox.onset = sandbox.onset;
    prms.disp_onset = 1;
    prms.hrf_pred=1;
    d = length(sandbox.onset);
    disp('Loading onset data...');
catch
    disp( 'Error loading onset file');
end

%~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Check if hemodynamics vector present
%~~~~~~~~~~~~~~~~~~~~~~~~~~~
try
    sandbox.hemodynamics = sandbox.hemodynamics;
    prms.disp_onset = 1;
    prms.hrf_pred=1;
    d = length(sandbox.hemodynamics);
    disp ('Loading hemodynamics data...');
catch
    disp ('Error loading hemodynamics file');
    sandbox.hemodynamics = zeros(1);
    prms.disp_onset = 1;
end

%~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Label sandbox.intrial if not yet labeled
%~~~~~~~~~~~~~~~~~~~~~~~~~~~
try

    if size(sandbox.intrial,2)<3
        sandbox.intrial = [sandbox.intrial(:,1) ones(size(sandbox.intrial,1),1)];
        disp ('Loading intrial data...');
        intrial2 = sandbox.intrial;
        intrial2(intrial2==0.01)=0;
        sandbox.intrial(:,3) = bwlabeln(intrial2(:,1));
    end

    if max(sandbox.intrial(:,2))<2;
        intrial2 = sandbox.intrial(:,3);
        for xi = 1: max(intrial2)
            temptrial = intrial2(intrial2==xi);
            temptrial2 = 1:length(temptrial);
            sandbox.intrial((sandbox.intrial(:,3)==xi),2)=temptrial2';
        end
    end

    disp (['Number of correct trials: ' num2str(max(sandbox.intrial(:,3)))]);
catch
    disp( 'Error loading intrial file');
end

%~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Try to load discard data
%~~~~~~~~~~~~~~~~~~~~~~~~~~~
try
    prms.discard = sandbox.discard;
    prms.disp_disc = 1;
    disp ('Loading discard data...');
catch
    prms.discard = zeros(d,1);
    disp( 'Error loading discard file');
end

%~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Check if full or concatenated scan
%~~~~~~~~~~~~~~~~~~~~~~~~~~~
try
    prms.fullscan = sandbox.fullscan;
    disp( 'Possibly no full scan, previously concatenated...');
catch
    prms. fullscan =1;
end

%~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Check if experiment data present
%~~~~~~~~~~~~~~~~~~~~~~~~~~~
try
    prms.experiment = sandbox.expinfo.expname;
    disp([ 'EXPERIMENT: ' experiment]);
catch
    disp( 'Error loading experiment information');
    prms.fullscan =1;
end

%~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Check if scan information present % THIS NEEDS TO BE FIXED PROPERLY!!!!
%~~~~~~~~~~~~~~~~~~~~~~~~~~~

if isfield(sandbox,'scanpar') && isfield(sandbox.scanpar,'segments')
disp(['Number of segments : '  num2str(sandbox.scanpar.segments)])
else
try
    try
    sandbox.scanpar.segments = sandbox.scanpar.acqp.PVM_EpiNShots;
    catch
        sandbox.scanpar.segments = sandbox.volmat.TR/sandbox.volmat.segmentTR;
    end
    disp(['Number of segments: ' num2str(sandbox.scanpar.segments)])
catch
    sandbox.scanpar.segments = 2;
    disp('Number of segments unknown, defaulting to 2-segment acquisition')
end
end

if ~isfield(sandbox, 'TR')
    if isfield(sandbox,'scanpar') && isfield(sandbox.scanpar,'TR')
        disp(['TR : '  num2str(sandbox.scanpar.TR)])
        sandbox.TR = sandbox.scanpar.TR;
    else
        try
            sandbox.TR = sandbox.scanpar.TR * sandbox.scanpar.segments/1000; %

            sb.scaninfo = sandbox.scanpar;
            disp(['TR: ' num2str(sandbox.TR)])
        catch
            disp('Issues loading TR/Segment information, defaulting to TR = 2s')
            sandbox.TR = 2;
        end
    end

end
try
    prms.TR = sandbox.TR;
    disp ('Loading TR...');
catch
    disp ('No TR data loaded, assuming TR=2 s');
    prms.TR = 2;
end

%~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Check if header information present
%~~~~~~~~~~~~~~~~~~~~~~~~~~~
try
    prms. hdr = sandbox.hdr;
    disp ('header data loaded');
catch
    hdr = load('hdr.mat');
    prms.hdr = hdr.hdr;
    disp ('sample header data loaded');
end

%~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Try to get header information from other fields
%~~~~~~~~~~~~~~~~~~~~~~~~~~~
try
    dim = size(idat);
    hdr.dime.dim (2:4) = dim(2:4);
catch
    disp ('Not all header information loaded');
end

%~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Try to load hemodynamics derivatives information
%~~~~~~~~~~~~~~~~~~~~~~~~~~~
try
    sandbox.hemodynamics_deriv = sandbox.hemodynamics_deriv;
    disp('Loading hemodynamics_deriv...');
catch
    disp ('No hemodynamics_deriv found');
end

end
